package com.ayuan.blog.service.impl;

import com.ayuan.blog.bean.WxSignInLogInfo;
import com.ayuan.blog.common.util.DateUtils;
import com.ayuan.blog.domain.WxUser;
import com.ayuan.blog.service.WxUserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.type.Alias;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.durcframework.core.service.CrudService;
import com.ayuan.blog.service.WxSignInLogService;
import com.ayuan.blog.dao.WxSignInLogDao;
import com.ayuan.blog.domain.WxSignInLog;
import com.ayuan.blog.domain.sch.WxSignInLogSch;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
    
@Service
public class WxSignInLogServiceImpl 
        extends CrudService<WxSignInLog, WxSignInLogDao> 
        implements WxSignInLogService {

    @Autowired
    private WxUserService wxUserService;

    @Override
    public PageInfo<WxSignInLog> findPage(WxSignInLogSch sch) {
        ExpressionQuery query = new ExpressionQuery();
        query.addPaginationInfo(sch);
        query.addAnnotionExpression(sch);

        long total = this.getDao().findTotalCount(query);
        List<WxSignInLog> list = this.getDao().find(query);

        PageInfo<WxSignInLog> page = new PageInfo<WxSignInLog>();
        page.setList(list);
        page.setPageNum(sch.getPageIndex()); // 设置当前页数
        page.setPageSize(sch.getPageSize()); // 设置每页的数量
        page.setSize(list.size()); // 设置当前页的数量
        page.setPages((int) ((total + sch.getPageSize() - 1) / sch.getPageSize())); // 设置总的页数
        page.setTotal(total); // 设置总的数量

        return page;
    }

    @Override
    public void addWxSignInLog(WxSignInLog wxSignInLog) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("wx_user_id", wxSignInLog.getWxUserId()));
        query.addValueExpression(new ValueExpression("sign_in_date", DateUtils.formatDate(wxSignInLog.getSignInDate(), "yyyy-MM-dd")));
        List<WxSignInLog> wxSignInLogList = find(query);
        if (CollectionUtils.isEmpty(wxSignInLogList)) {
            save(wxSignInLog);
        }
    }

    @Override
    public List<WxSignInLog> getTodaySignInLogList() {
        ExpressionQuery query = new ExpressionQuery();
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date today = calendar.getTime();
        query.addValueExpression(new ValueExpression("sign_in_date", today));
        query.addSort("sign_in_date");

        return find(query);
    }

    @Override
    public List<WxSignInLogInfo> getTodaySignInLogInfoList() {
        List<WxSignInLog> wxSignInLogList = getTodaySignInLogList();
        List<WxSignInLogInfo> wxSignInLogInfoList = new ArrayList<WxSignInLogInfo>();
        if (CollectionUtils.isNotEmpty(wxSignInLogList)) {
            for (WxSignInLog wxSignInLog : wxSignInLogList) {
                WxSignInLogInfo wxSignInLogInfo = new WxSignInLogInfo();
                WxUser wxUser = wxUserService.get(wxSignInLog.getWxUserId());
                wxSignInLogInfo.setNickName(wxUser.getNickname());
                wxSignInLogInfo.setHeadImgUrl(wxUser.getHeadImgUrl());
                BeanUtils.copyProperties(wxSignInLog, wxSignInLogInfo);
                wxSignInLogInfoList.add(wxSignInLogInfo);
            }
        }

        return wxSignInLogInfoList;
    }

    @Override
    public WxSignInLog getTodaySignInLogByWxUserId(Long wxUserId) {
        return getOneDaySignInLog(wxUserId, 0);
    }

    private WxSignInLog getOneDaySignInLog(Long wxUserId, int day) {
        ExpressionQuery query = new ExpressionQuery();
        query.addValueExpression(new ValueExpression("wx_user_id", wxUserId));
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - day, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date today = calendar.getTime();
        query.addValueExpression(new ValueExpression("sign_in_date", today));

        List<WxSignInLog> wxSignInLogList = find(query);

        if (CollectionUtils.isNotEmpty(wxSignInLogList)) {
            return wxSignInLogList.get(0);
        }

        return null;
    }

    @Override
    public String getSignInMessage(Long wxUserId) {
        WxSignInLog todayWxSignInLog = getOneDaySignInLog(wxUserId, 0);
        WxSignInLog yesterdayWxSignInLog = getOneDaySignInLog(wxUserId, 1);
        if (null == todayWxSignInLog) {
            return null;
        }
        String message = null;
        message = String.format("今天的签到时间为 %s ^ _ ^", DateUtils.formatDate(todayWxSignInLog.getSignInTime(), "HH:mm"));
        if (null != yesterdayWxSignInLog) {
            long difMillionSecond = todayWxSignInLog.getSignInTime().getTime()
                    - yesterdayWxSignInLog.getSignInTime().getTime()
                    - 24 * 60 * 60 * 1000;

            long difSecond =  Math.abs(difMillionSecond) / 1000 % 60;
            long difMinute = Math.abs(difMillionSecond) / 1000 / 60 % 60;
            long difHour = Math.abs(difMillionSecond) / 1000 / 60 / 60;
            if (difMillionSecond > 0) {
                message += "\n比昨日迟了";
            } else if (difMillionSecond < 0){
                message += "\n比昨天早了";
            } else {
                message += "\n和昨天一样一样的";
            }
            if (difHour > 0) {
                if (difMinute > 0) {
                    message += String.format("%s小时%s分钟", difHour, difMinute);
                } else {
                    message += String.format("%s小时整", difHour);
                }
            } else {
                if (difMinute > 0) {
                    if (difSecond > 0) {
                        message += String.format("%s分%s秒", difMinute, difSecond);
                    } else {
                        message += String.format("%s分钟", difMinute);
                    }
                } else {
                    message += String.format("%s秒", difSecond);
                }
            }
        }
        return message;
    }
}